package com.eleadmin.ocean.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.ocean.biz.OceanBulkCargoFeeAttachBiz;
import com.eleadmin.ocean.entity.OceanBulkCargoFeeAttach;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.order.result.SelectResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2024-08-05 17:33:17
 * @version ：1.0
 */
@Repository
public class OceanBulkCargoFeeAttachBizImpl extends BaseDao implements OceanBulkCargoFeeAttachBiz {

    @Override
    public JsonResult list(PageResult2<T> page, OceanBulkCargoFeeAttach oceanBulkCargoFeeAttach, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<OceanBulkCargoFeeAttach> rm = new BeanPropertyRowMapper<OceanBulkCargoFeeAttach>(OceanBulkCargoFeeAttach.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.name");
        sql.append(",o.pinyin,o.price,o.at_modules,o.type");
		sql.append(",o.remark,o.create_time,o.create_user,o.update_time");
		sql.append(",o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_ocean_bulk_cargo_fee_attach o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=o.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(oceanBulkCargoFeeAttach)){
            if(Utils.isNotEmpty(oceanBulkCargoFeeAttach.getName())){
                sql.append(" and instr(o.name,:name)>0 ");
                map.put("name", oceanBulkCargoFeeAttach.getName());
            }
            if(Utils.isNotEmpty(oceanBulkCargoFeeAttach.getAtModules())){
                sql.append(" and o.at_modules=:at_modules ");
                map.put("at_modules", oceanBulkCargoFeeAttach.getAtModules());
            }
            if(Utils.isNotEmpty(oceanBulkCargoFeeAttach.getSort())&&Utils.isNotEmpty(oceanBulkCargoFeeAttach.getOrder())){
                sql.append(" order by o."+oceanBulkCargoFeeAttach.getSort()+" "+oceanBulkCargoFeeAttach.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<OceanBulkCargoFeeAttach> list(OceanBulkCargoFeeAttach oceanBulkCargoFeeAttach, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<OceanBulkCargoFeeAttach> rm = new BeanPropertyRowMapper<OceanBulkCargoFeeAttach>(OceanBulkCargoFeeAttach.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.name");
        sql.append(",o.pinyin,o.price,o.at_modules,o.type");
		sql.append(",o.remark,o.create_time,o.create_user,o.update_time");
		sql.append(",o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean_bulk_cargo_fee_attach o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(oceanBulkCargoFeeAttach)){
            if(Utils.isNotEmpty(oceanBulkCargoFeeAttach.getName())){
                sql.append(" and instr(o.name,:name)>0 ");
                map.put("name", oceanBulkCargoFeeAttach.getName());
            }
            if(Utils.isNotEmpty(oceanBulkCargoFeeAttach.getAtModules())){
                sql.append(" and o.atModules=:atModules ");
                map.put("atModules", oceanBulkCargoFeeAttach.getAtModules());
            }
            if(Utils.isNotEmpty(oceanBulkCargoFeeAttach.getSort())&&Utils.isNotEmpty(oceanBulkCargoFeeAttach.getOrder())){
                sql.append(" order by o."+oceanBulkCargoFeeAttach.getSort()+" "+oceanBulkCargoFeeAttach.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public List<SelectResult> listSel(OceanBulkCargoFeeAttach oceanBulkCargoFeeAttach, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select o.id value,o.name label");
        sql.append(",o.price,o.at_modules module,o.type");
        sql.append(" from chf_ocean_bulk_cargo_fee_attach o ");
        sql.append(" where o.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(oceanBulkCargoFeeAttach)){
            if(Utils.isNotEmpty(oceanBulkCargoFeeAttach.getName())){
                sql.append(" and instr(o.name,:name)>0 ");
                map.put("name", oceanBulkCargoFeeAttach.getName());
            }
            if(Utils.isNotEmpty(oceanBulkCargoFeeAttach.getAtModules())){
                sql.append(" and o.atModules=:atModules ");
                map.put("atModules", oceanBulkCargoFeeAttach.getAtModules());
            }
            if(Utils.isNotEmpty(oceanBulkCargoFeeAttach.getSort())&&Utils.isNotEmpty(oceanBulkCargoFeeAttach.getOrder())){
                sql.append(" order by o."+oceanBulkCargoFeeAttach.getSort()+" "+oceanBulkCargoFeeAttach.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public OceanBulkCargoFeeAttach getById(String id) {
        RowMapper<OceanBulkCargoFeeAttach> rm = new BeanPropertyRowMapper<OceanBulkCargoFeeAttach>(OceanBulkCargoFeeAttach.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.name");
        sql.append(",o.pinyin,o.price,o.at_modules,o.type");
		sql.append(",o.remark,o.create_time,o.create_user,o.update_time");
		sql.append(",o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean_bulk_cargo_fee_attach o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 and o.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        try {
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean save(OceanBulkCargoFeeAttach oceanBulkCargoFeeAttach, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into chf_ocean_bulk_cargo_fee_attach (");
        sql.append("organization_id,name,pinyin,price");
		sql.append(",at_modules,type,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:pinyin,:price");
		sql.append(",:at_modules,:type,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", oceanBulkCargoFeeAttach.getName());
        map.put("pinyin", oceanBulkCargoFeeAttach.getPinyin());
        map.put("price", oceanBulkCargoFeeAttach.getPrice());
        map.put("at_modules", oceanBulkCargoFeeAttach.getAtModules());
        map.put("type", oceanBulkCargoFeeAttach.getType());
        map.put("remark", oceanBulkCargoFeeAttach.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(OceanBulkCargoFeeAttach oceanBulkCargoFeeAttach, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update chf_ocean_bulk_cargo_fee_attach set ");
        sql.append("name=:name,pinyin=:pinyin,price=:price ");
		sql.append(",at_modules=:at_modules,type=:type,remark=:remark ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", oceanBulkCargoFeeAttach.getName());
        map.put("pinyin", oceanBulkCargoFeeAttach.getPinyin());
        map.put("price", oceanBulkCargoFeeAttach.getPrice());
        map.put("at_modules", oceanBulkCargoFeeAttach.getAtModules());
        map.put("type", oceanBulkCargoFeeAttach.getType());
        map.put("remark", oceanBulkCargoFeeAttach.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", oceanBulkCargoFeeAttach.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(id.length()>0){
            for(String id1 : id.split(",")){
                map  = new HashMap<String, String>();
                if(id1==null || id1.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update chf_ocean_bulk_cargo_fee_attach set deleted=1" +
                    ",update_user=:update_user, update_time=now() "
                    + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}
